package com.jiazhong.example.project.bookstore.front.controller;

import com.jiazhong.example.project.bookstore.commons.util.JsonResult;
import com.jiazhong.example.project.bookstore.front.service.ShoppingCartService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/shopping_cart")
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;

    @PostMapping("/save/{bookId}")
    public JsonResult save(@PathVariable("bookId") String bookId, HttpServletRequest request) {
        return shoppingCartService.addCart(bookId, request);
    }

    @GetMapping
    public JsonResult find(HttpServletRequest request) {
        return shoppingCartService.findCartByUserId(request);
    }

    @PutMapping("/change_number/{bookId}/{number}")
    public JsonResult updateNumber(@PathVariable("bookId") String bookId, @PathVariable("number") int number, HttpServletRequest request) {
        if (number == 1) {
            return shoppingCartService.updateCartNumberIncrement(bookId, request);
        } else if (number == -1) {
            return shoppingCartService.updateCartNumberDecrement(bookId, request);
        } else {
            return null;
        }
    }

    @PutMapping("/change_number2/{bookId}/{number}")
    public JsonResult updateNumber2(@PathVariable("bookId") String bookId, @PathVariable("number") int number, HttpServletRequest request) {
        return shoppingCartService.updateCartNumber(bookId, number, request);
    }

    @DeleteMapping("/remove_cart/{bookId}")
    public JsonResult removeCart(@PathVariable("bookId") String bookId, HttpServletRequest request) {
        return shoppingCartService.removeCart(bookId, request);
    }

    @PostMapping("/remove_cart_batch")
    public JsonResult removeCartBatch(String[] bookIds, HttpServletRequest request) {
        log.info("bookIds: {}", Arrays.toString(bookIds));
        return shoppingCartService.removeCartBatch(bookIds, request);
    }

}
